<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title></title>
<#include "head.ftl" />
</head>
<body>
<div class="body-box">
<div class="rhead">
	<div class="rpos">当前位置: 奖品 - 列表</div>
	<div class="clear"></div>
</div>
<div style="padding:10px;">
	<div>
	<form id="search_from">
	<input type="text" name="searchWord" value="${searchWord}" placeholder="名称">
	<input type="submit" value="查找">
	</form>
	</div>
	<div class="clear"></div>
</div>
<form id="tableForm" method="post">
<table class="pn-ltable" style="" width="100%" cellspacing="1" cellpadding="0" border="0" >
<thead class="pn-lthead"><tr>
	<th width="20"><input type='checkbox' onclick='Pn.checkbox("ids",this.checked)'/></th>
	<th>ID</th>
	<th>类型</th>
	<th>名称</th>
	<th>价值</th>
	<th>数量</th>
	<th>状态</th>
	<th>中奖率</th>
	<th>描述</th>
	<th>有效期限 /天</th>
	<th>操作</th></tr></thead>
<tbody  class="pn-ltbody" align="center">
<#list prizeList as prize>
<tr id="tr_${prize.prizeId}">
	<td><input type="checkbox" id="check_${prize.prizeId}"/></td>
	<td>${prize.prizeId}</td>
	<td>${prizeTypes[prize.type].text}</td>
	<td><a href="javascript:;" id="a_${prize.prizeId}_name" >${prize.name}</a></td>
	<td><a href="javascript:;" id="a_${prize.prizeId}_worth">${prize.worth}</a></td>
	<td><a href="javascript:;" id="a_${prize.prizeId}_amount">${prize.amount}</a></td>
	<td>
	 <select id="select_${prize.prizeId}_status_${prize.status}">
	 <#list prizeStatuses as prizeStatus>
	  <option value="${prizeStatus.value}" <#if prize.status == prizeStatus.value>selected</#if> >${prizeStatus.text}</option>
	 </#list>
	</select>
	</td>
	<td><a href="javascript:;" id="a_${prize.prizeId}_rate">${prize.rate}</a></td>
	<input type="hidden" id="totalRate_${prize.prizeId}" status="${prize.status}" value="${prize.rate}"/>
	<td><a href="javascript:;" id="a_${prize.prizeId}_summary">${prize.summary}</a></td>
	<td><a href="javascript:;" id="a_${prize.prizeId}_expiredDay">${prize.expiredDay}</a></td>
	<td align="center"><a href="javascript:;" class="pn-opt" id="removeBt_${prize.prizeId}_${prize.name}">删除</a></td>
</tr>
</#list>
</tbody>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="center" class="pn-sp">
	<div id="pageLink" class="pageLinks">${pageLink.linkString}</div>
</td></tr></table>
</form>
<div>
	<label>奖品类型：</label>
	<select id="newType">
	 <#list prizeTypes as type>
	  <option value="${type.value}" >${type.text}</option>
	 </#list>
	</select>
	<input type="button" value="添加" id="addPrizeBt"/>
</div>
</div>
</body>
<script type="text/javascript">
	// 添加奖品请求地址
	var addPrizePath = g_domain + "/prize/addDo/json";
	// 修改奖品参数请求地址
	var updatePrizePath = g_domain + "/prize/updateDo/json";
	//  删除奖品请求地址
	var removePrizePath = g_domain + "/prize/removeDo/json";

	// 事件:添加奖品
	$("#addPrizeBt").click(function(){
		$.post(addPrizePath, {type: $("#newType").val()}, function(data){
			if (data.status !== 0) {
				alert(data.message);
				return;
			} 
			location.href = g_domain + "/prize/list";
		});
	});
	
	// 事件:修改奖品参数
	$(document).on("click", "[id^='a_']", function(){
		var $this = $(this);
		var val = $this.html();
		var datas = $this.attr("id").split("_");
		$this.replaceWith("<input type='text' id='input_" + datas[1] + "_" + datas[2] + "' value='" + val + "' style='width:100px;'/>");
	});
	
	// 事件:提交奖品参数
	$(document).on("blur", "[id^='input_']", function(){
		var $this = $(this);
		var datas = $this.attr("id").split("_");
		var val = $this.val();
		var param ="{prizeId:" + datas[1] + "," + datas[2] + ":\"" + val + "\"}";
		var prize = null;
		eval("prize=" + param);
		$.post(updatePrizePath, prize, function(data){
			if (data.status !== 0) {
				alert(data.message);
				return;
			}
			$this.replaceWith("<a href='javascript:;' id='a_" + datas[1] + "_" + datas[2] + "'>" + val + "</a>");
			// '中奖率'参数提交后需要统计'上架'奖品的总中奖率是否为100%
			if (datas[2] == "rate") {
				$("#totalRate_" + datas[1]).val(val);
				sumRate();
			}
		});
	});
	
	// 事件:更改奖品下拉选参数
	$("[id^='select_']").change(function(){
		var $this = $(this);
		var datas = $this.attr("id").split("_");
		var val = $this.val();
		var param ="{prizeId:" + datas[1] + "," + datas[2] + ":" + val + "}";
		var prize = null;
		eval("prize=" + param);
		$.post(updatePrizePath, prize, function(data){
			if (data.status !== 0) {
				alert(data.message);
				$this.val(datas[3]);
				return;
			} 
			if (datas[2] == "status") {
				$("#totalRate_" + datas[1]).attr("status", val);
				sumRate();
			}
		});
	});
	
	// 统计'上架'奖品的总中奖率
	function sumRate(){
		var totalRate = 0;
		$("[status='1']").each(function(){
			var rate = $(this).val();
			rate = parseFloat(rate);
			totalRate += rate;
		});
		// 若'上架'奖品的总中奖率不为100%，弹出提示框
		var percentage = (totalRate * 100);
		if (percentage.toFixed(0) != 100) {
			alert("\"上架\"奖品的总中奖率目前是" + percentage + "%,不是100%，请重新设置\"上架\"奖品的中奖率");
		}
	}
	
	// 事件: 删除奖品
	$(document).on("click", "[id^='removeBt_']", function(){
		var $this = $(this);
		var data = $this.attr("id").split("_");
		var name = data[2];
		var prizeId = data[1];
		if (confirm("确定要删除  \"" + name + "\" 这条记录吗?")) {
			$.post(removePrizePath, {prizeId:prizeId}, function(data){
				if (data.status === 0) {
					alert(data.message);
					return;
				} 
				$("#tr_" + prizeId).remove();
			});
		}
	});
	
</script>
</html>